// pages/paihang/paihang.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    syWidth: "",//系统宽度
    syHeight: "",//系统高度
    ggtjData: [
    
    ],
    myData: [

    ],
    userwxid:"",//微信ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    //获取设备宽高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          syWidth: res.windowWidth,
          syHeight: res.windowHeight,
        })
      }
    })
 
    that.getuserwxid();//获取wxid

  },
  //获取用户wxid
  getuserwxid: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log("CODE:" + res.code)
          //发起网络请求
          wx.request({
            url: 'https://www.citytk.com/citytk/mgbs/modeht/code.php',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              code: res.code
            },
            method: "POST",
            success: function (res) {

              if (res.data != "nowxid") {

                app.globalData.userwxid = res.data;
                var _wxid = app.globalData.userwxid;
                if (_wxid != "") {
                  that.setData({
                    userwxid: _wxid,
                  })
                  that.paihangclick();//拉取排行榜
                }
                console.log("获得用户WXID:" + app.globalData.userwxid)

              } else {
                console.log('没有获取到wxid' + res.errMsg)
              }
            },
            fail: function (e) {
              console.log("用户登录失败:" + e.errMsg)
            }
          })
        } else {

          console.log('登录失败！' + res.errMsg)
        }
      }
    });

  },
  //拉取排行榜
paihangclick:function(){
    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "cxpaihang",
      },
      method: "POST",
      success: function (res) {
        console.log("获取排行榜数据：" + res.data.length)
    
        if (res.data != "") {

         
          var ggarr = [];
          console.log("增加数据")
          var i = 0;
          for(i;i<10;i++){
            var objarr = {};
             objarr.viewId=i+1;
             objarr.xingming = res.data[i].xingming;
             objarr.fenshu = res.data[i].fenshu;
             objarr.shijian = res.data[i].shijian/100+"秒";
          ggarr.push(objarr);
          }
          console.log("数组长度" + ggarr.length)
          that.setData({
            ggtjData: ggarr,
          })
          var myarr = [];
          console.log("增加数据" + that.data.userwxid)
          var t = 0;

          for (t; t < res.data.length; t++) {
            if (that.data.userwxid == res.data[t].wxid) {
              var objarr = {};
              objarr.viewId = t + 1;
              objarr.xingming = res.data[t].xingming;
              objarr.fenshu = res.data[t].fenshu;
              objarr.shijian = res.data[t].shijian / 100 + "秒";
              myarr.push(objarr);
              }
          
          }
       
          that.setData({
            myData: myarr,
          })
          console.log("获取我的数据" + myarr.length )

        } else {
        
          console.log("数据库没有数据")
        }
      },
      fail: function (e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })


},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  
})